I study pThread, and now I have problem with sending arg (struct data type) to new thread...
In terminal I have this output:
grytskiv@ZXDSL831II:~/thread/$ gcc -l pthread scaner.c -o scaner && ./scaner
In main: creating thread 0
In child: creating thread 0
�:10072052 ok
grytskiv@ZXDSL831II:~/thread/$
but if i call function scanner_port(void *thread_arg) whitout pthread_create(), my function work successful!
I can't find error...
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
struct address
{
char *ip;
int port;
};
void *
scanner_port(void *thread_arg)
{
struct address *address_pc;
address_pc = (struct address *) thread_arg;
printf("%s:%i\tok\n",address_pc->ip,address_pc->port);
exit(0);
}
void *
scanner_ip(char *ip)
{
int port_list[19];
struct address address_pc;
address_pc.ip = ip;
port_list[0] = 80;
port_list[1] = 8080;
pthread_t threads[2];
int rc;
long t;
for(t=0; t<1; t++)
{
address_pc.port = port_list[t];
printf("In child: creating thread %ld\n", t);
rc = pthread_create(&threads[t],NULL,(void*)scanner_port,(void *) &address_pc);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
//scanner_port((void *) &address_pc);
}
pthread_exit(NULL);
}
int
main()
{
char *ip;
int ip_count = 1;
ip="127.0.0.1";
pthread_t threads[ip_count];
int rc;
long t;
for(t=0; t<ip_count; t++)
{
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t],NULL,(void*)scanner_ip,ip);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}